//
//  EMFeedbackViewController.m
//  ElectronicMall
//
//  Created by 史伟文 on 14/12/26.
//  Copyright (c) 2014年 cczu. All rights reserved.
//

#import "EMFeedbackViewController.h"
#import "UINavigationController+EMNavi.h"
#import "EMFeedbackReqBody.h"
#import "EMFeedbackRespBody.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "UserInfo.h"

@interface EMFeedbackViewController ()
{
    UITextView *tipTextView;
    AppDelegate *app;
}
@end

@implementation EMFeedbackViewController
- (void)hideBottomBar
{
    for(UIView *view in self.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
            view.hidden = YES;
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self hideBottomBar];
    self.view.backgroundColor = RGB(242, 242, 242);
    [self.navigationController setNavigationMode:EMNavigationModeDefault andSubMode:0];
    self.navigationItem.title = @"意见反馈";
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 30, 32);
    [button setImage:[UIImage imageNamed:@"button_导航_返回_白"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:button];
}
- (void)backAction: (UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    int height = 15 + 64;
    
    
    
    tipTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, height, W(300), 250)];
    tipTextView.layer.borderWidth = 1;
//    tipTextView.layer.borderColor = [UIColor grayColor].CGColor;
    tipTextView.layer.cornerRadius = 5;
//    tipTextView.text = [NSString stringWithFormat:@"请提出宝贵意见"];
    tipTextView.backgroundColor = [UIColor clearColor];
    tipTextView.textAlignment = NSTextAlignmentLeft;
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = 10;
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont fontWithName:HEITILIGHT size:15],
                                 NSParagraphStyleAttributeName: paraStyle,
                                 NSForegroundColorAttributeName: RGB(200, 200, 200),
                                 
                                 };
    tipTextView.attributedText = [[NSAttributedString alloc] initWithString:tipTextView.text attributes:attributes];
    tipTextView.delegate = self;
    tipTextView.contentInset = UIEdgeInsetsMake(-64, 10, 15, 15);
    tipTextView.font = [UIFont fontWithName:HEITILIGHT size:15];
    [tipTextView becomeFirstResponder];
    [self.view addSubview:tipTextView];
    
    
    height += 250 + 30;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(10, height, W(300), 40);
    btn.backgroundColor = RGB(112, 179, 81);
    btn.layer.cornerRadius = 5;
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [btn setTitle:@"确认提交" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(handupAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIImageView *logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_logo"]];
    logoImageView.frame = CGRectMake(0, 0, 96, 18);
    logoImageView.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height - 36);
    [self.view addSubview:logoImageView];
    
    UILabel *logoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 24, [UIScreen mainScreen].bounds.size.width, 20)];
    logoLabel.font = [UIFont fontWithName:HEITILIGHT size:10];
    logoLabel.textColor = [UIColor grayColor];
    logoLabel.textAlignment = NSTextAlignmentCenter;
    logoLabel.text = @"Copyright © 2014 wojiaxiansheng.com. All Right Reserved";
    [self.view addSubview:logoLabel];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackground:)];
    [self.view addGestureRecognizer:tapGR];

}


- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [tipTextView resignFirstResponder];
    return YES;
}
#pragma mark - 关闭键盘
- (void)tapBackground:(UIGestureRecognizer *)recog
{
    [tipTextView resignFirstResponder];
}

- (void)handupAction:(UIButton *)sender
{
    
    [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
    EMFeedbackReqBody *reqBody = [[EMFeedbackReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"FEEDBACK-OPTION";
    reqBody.USERID = [UserInfo sharedUserInfo].m_userName;
    reqBody.SESSION = [UserInfo sharedUserInfo].m_session;
    reqBody.OPTION = tipTextView.text;
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        EMFeedbackRespBody *respBody = [EMFeedbackRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            
            [app showTip:respBody.SUCCESSFULINFO];
            [self.navigationController popViewControllerAnimated:YES];
            
        } else {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];
}
- (void)showAlert:(NSString *)content
{
    EMAttention *attention = [[EMAttention alloc] initWithTitle:@"提示" contentText:content leftButtonTitle:nil rightButtonTitle:@"确定"];
    [attention show];
}
@end
